<?php
namespace app\tripapi\controller;
use app\tripapi\model\TravelPlan;
use app\tripapi\model\TravelApply;
use app\tripapi\model\TravelApplyCustomer;
use think\Controller;
use think\Request;
class Travel extends ApiBaseController
{
    private $TravelPlan;
    private $TravelApply;
    private $TravelApplyCustomer;

    public function __construct()
    {
        parent::__construct();
        $this->departmentid = $_SESSION['LOGIN_DEPT_ID'];
        $this->uid = $_SESSION['LOGIN_UID']; // 填报人id
        $this->uname = str_replace(',', '', iconv('gbk', 'UTF-8', GetUserNameByUid($this->uid))); // 填报人姓名
        $this->TravelPlan = new TravelPlan();
        $this->TravelApply = new TravelApply();
        $this->TravelApplyCustomer = new TravelApplyCustomer();
    }

    /*
     * 出差申请单列表页
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 出差申请单列表页
     */
    public function index_hr()
    {
        return $this->fetch();
    }

    /*
     * 出差申请单列表页
     */
    public function index_fc()
    {
        return $this->fetch();
    }

    /*
     * 出差申请单列表页
     */
    public function index_mg()
    {
        return $this->fetch();
    }

    /*
     * 出差申请单列表页
     */
    public function index_pt()
    {
        return $this->fetch();
    }

    /*
     * 出差申请单Ajax列表
     */
    public function GetListByAjax()
    {
        if($this->params['nid'] == 'null'){
            $nid = '';
        }else{
            $nid = $this->params['nid'];
        }
        $data['page'] = $this->params['page'];
        $data['limit'] = $this->params['limit'];
        $travelplan = $this->TravelPlan->GetDetail($this->params['id']);
        $travel_plan = $this->TravelApply->GetApplyId($travelplan['0']['plan_num'],$this->uid, $data, $nid); // 获取apply id
        if(isset($travel_plan['0'])){
            $count = $travel_plan['0']['count'];
        }else{
            $count = "0";
        }
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $travel_plan);
        return json_api($json);
    }

    /*
     *  行政 / 财务审批列表
     */
    public function GetOtherListByAjax(){
        if($this->params['nid'] == 'null'){
            $nid = '';
        }else{
            $nid = $this->params['nid'];
        }
        $data['page'] = $this->params['page'];
        $data['limit'] = $this->params['limit'];
        $uid = $this->uid;
        $result = $this->TravelApply->GetOtherList($data,$uid, $nid);
        $count = count($result);
        $json = array("status" => 200, "msg" => "获取成功", "count" => $count, "data" => $result);
        return json_api($json);
    }

    /*
     * 出差申请单添加
     */
    public function add()
    {
        $travel_num_list = $this->TravelPlan->where('user_id', '=', $_SESSION['LOGIN_UID'])->select();
        $this->assign('travel_num_list', $travel_num_list);
        return $this->fetch();
    }

    /*
     * 出差申请单查看
     */
    public function show()
    {
        $id = $_GET['id'];
        $result = $this->TravelApply->GetItemById($id);
        $customer_item = $this->TravelApplyCustomer->GetCustomerItemById($id);
        $this->assign('traffic_self', json_decode($result['0']['traffic_self']));
        $this->assign('business_receive', json_decode($result['0']['business_receive']));
        $this->assign('budget_item', json_decode($result['0']['budget_item']));
        $this->assign('customer_item', $customer_item);
        $approve = $this->TravelApply->ShowInput($id, $this->uid);
        $approvel = $this->TravelApply->ShowButton($id, $this->uid);
        $this->assign('isshow', $approve);
        $this->assign('itemdetail', $result);
        return $this->fetch();
    }

    /*
     *  出差申请单编辑
     */
    public function edit()
    {
        $id = $_GET['id'];
        $result = $this->TravelApply->GetItemById($id);
        $customer_item = $this->TravelApplyCustomer->GetCustomerItemById($id);
        $this->assign('traffic_self', json_decode($result['0']['traffic_self']));
        $this->assign('business_receive', json_decode($result['0']['business_receive']));
        $this->assign('budget_item', json_decode($result['0']['budget_item']));
        $this->assign('customer_item', $customer_item);
        $this->assign('itemdetail', $result);
        return $this->fetch();
    }

    /*
     *  出差申请单保存
     */
    public function Save()
    {
//        print_r($this->params);exit;
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $this->TravelApply->TravelApplySave($this->params,$this->uid);
        if ($id['id']) {
            $this->TravelApplyCustomer->TravelApplyCustomerSave($id['id'], $this->params);
            $retInfo = array("status" => 200, "msg" => '申请成功', "data" => $id);
        } else {
            $retInfo = array("status" => 500, "msg" => '申请失败', "data" => array());
        }
        return json_api($retInfo);
    }

    /*
     * 删除
     */
    public function Delete()
    {
        $id = $_GET['id'];
        $result = $this->TravelApply->ApplyDelete($id);
        if ($result) {
            $this->TravelApplyCustomer->ApplyCustomerDelete($id);
            $retInfo = array("status" => 200, "msg" => "删除成功", "data" => array());
            return json_api($retInfo);
        } else {
            $retInfo = array("status" => 500, "msg" => "删除失败", "data" => array());
            return json_api($retInfo);
        }
    }

    /*
     * 更新
     */
    public function Update()
    {
        $this->params['creator'] = $this->uname; // 创建人
        $this->params['creator_id'] = $this->uid; // 创建人id
        $id = $_GET['id'];
        $result = $this->TravelApply->ApplyUpdate($id, $this->params);
        if ($result) {
            $this->TravelApplyCustomer->ApplyCustomerUpdate($id, $this->params);
            $retInfo = array("status" => 200, "msg" => "更新成功", "data" => array());
            return json_api($retInfo);
        } else {
            $retInfo = array("status" => 500, "msg" => "更新失败", "data" => array());
            return json_api($retInfo);
        }
    }

    /*
     * 审批
     */
    public function ApproveTo(){
        $uid = $this->uid;
        $data['id'] = $this->params['id'];
        if(!empty($this->params['depart_opinion'])){
            $data['depart_opinion'] = iconv('UTF-8', 'GBK',$this->params['depart_opinion']);
        }else{
            $data['depart_opinion'] = '';
        }
        if(!empty($this->params['politic_opinion'])){
            $data['politic_opinion'] = iconv('UTF-8', 'GBK',$this->params['politic_opinion']);
        }else{
            $data['politic_opinion'] = '';
        }
        if(!empty($this->params['finance_opinion'])){
            $data['finance_opinion'] = iconv('UTF-8', 'GBK',$this->params['finance_opinion']);
        }else{
            $data['finance_opinion'] = '';
        }
        if(!empty($this->params['manager_opinion'])){
            $data['manager_opinion'] = iconv('UTF-8', 'GBK',$this->params['manager_opinion']);
        }else{
            $data['manager_opinion'] = '';
        }
        $data['finance_total'] = $this->params['finance_total'];
        $result = $this->TravelApply->ApproveTo($data,$uid);
        $retInfo = array("status" => 200, "msg" => "审批成功", "data" => $result);
        return json_api($retInfo);
    }
}